package com.nuttysoft.nutand.views;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.nuttysoft.nutand.utils.DisplayUtils;
import com.nuttysoft.nutand.utils.ViewUtil;
import com.squareup.picasso.Picasso;

/**
 * Created by Administrator on 2016/3/10 0010.
 */
public class TopBar extends RelativeLayout {

    private Context context;

    /**
     * 左icon的布局参数
     * */
    private RelativeLayout.LayoutParams leftIconParams ;

    /**
     * 右icon的布局参数
     * */
    private RelativeLayout.LayoutParams rightIconParams;

    /**
     * 中间title的布局参数
     * */
    private RelativeLayout.LayoutParams centerTitleParams;

    /**
     *左侧icon
     * */
    private ImageView leftIcon;

    /**
     *右侧icon
     * */
    private ImageView rightIcon;

    /**
     *title
     * */
    private TextView title;

    public TopBar(Context context) {
        super(context);
        this.context = context;
        initView();
    }

    public TopBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        initView();
    }

    public TopBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        initView();
    }

    public void initView(){

        //初始化布局参数
        leftIconParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT);
        leftIconParams.addRule(ALIGN_PARENT_LEFT);
        leftIconParams.addRule(CENTER_VERTICAL);

        rightIconParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT);
        rightIconParams.addRule(ALIGN_PARENT_RIGHT);
        rightIconParams.addRule(CENTER_VERTICAL);
        centerTitleParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
        centerTitleParams.addRule(CENTER_IN_PARENT);
        //初始化布局
        leftIcon = new ImageView(context);
        leftIcon.setPadding((int)ViewUtil.dip2px(context,16),0,(int)ViewUtil.dip2px(context,16),0);
        rightIcon = new ImageView(context);
        rightIcon.setPadding((int)ViewUtil.dip2px(context,16),0,(int)ViewUtil.dip2px(context,16),0);
        title = new TextView(context);
        //填充布局
        this.addView(leftIcon,leftIconParams);
        this.addView(rightIcon,rightIconParams);
        this.addView(title,centerTitleParams);
    }

    /**
     * 设置标题
     * **/
    public void setTitle(String title){
        this.title.setText(title);
    }

    /**
     * 设置标题颜色
     * @param hexClr 十六进制的颜色字符串
     * */
    public void setTitleColor(String hexClr ){
        this.title.setTextColor(Color.parseColor(hexClr));
    }

    /**
     * 设置标题大小
     * @param size 直接填入sp为单位的大小
     * */
    public void setTitleSize(float size){
        this.title.setTextSize(size);
    }

    /**
     * 设置左侧icon
     * @param
     * */
    public void setLeftIcon(int resId){
        Picasso.with(context).load(resId).into(leftIcon);
    }

    /**
     * 设置右侧icon
     * @param
     * */
    public void setRightIcon(int resId){
        Picasso.with(context).load(resId).into(rightIcon);
    }

    /**
     * 设置左侧icon缩放版
     * @param
     * */
    public void setScaledLeftIcon(int resId,int w,int h){
        Picasso.with(context).
                load(resId).
                resize((int)ViewUtil.dip2px(context, w),(int) ViewUtil.dip2px(context, h)).
                centerCrop().
                into(leftIcon);
    }

    /**
     * 设置右侧icon设置左侧icon缩放版
     * @param
     * */
    public void setScaledRightIcon(int resId,int w,int h){
        Picasso.with(context).
                load(resId).
                resize((int)ViewUtil.dip2px(context, w),(int) ViewUtil.dip2px(context, h)).
                centerCrop().
                into(rightIcon);
    }

    /**
     * 设置leftIcon的leftmargin
     * @param margin 直接使用dp值
     * */
    public void setLetfIconLeftMargin(float margin){
        leftIconParams.setMargins((int) ViewUtil.dip2px(context,margin),0,0,0);
    }

    /**
     * 设置右侧iconmargin
     * */
    public void setRightIconRightMargin(float margin){
        rightIconParams.setMargins(0,0,(int) ViewUtil.dip2px(context,margin),0);
    }

    /**
     * 设置左侧icon是否显示
     * @param  isVisible 是否显示
     * */
    public void setLeftIconVisible(boolean isVisible){
        if(isVisible){
            leftIcon.setVisibility(View.VISIBLE);
        }else {
            leftIcon.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 设置右侧icon是否显示
     * @param  isVisible 是否显示
     * */
    public void setRightIconVisible(boolean isVisible){
        if(isVisible){
            rightIcon.setVisibility(View.VISIBLE);
        }else {
            rightIcon.setVisibility(View.GONE);
        }
    }

    /**
     * 设置topbar背景，临时只采用设置资源文件
     * @param resId 背景色资源
     * */
    public void setTopBackGround(int resId){
        this.setBackgroundResource(resId);
    }

    public ImageView getLeftIcon(){
        return leftIcon;
    }

    public ImageView getRightIcon(){
        return rightIcon;
    }
}
